package com.topc.participant.api;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import com.topc.base.api.CommonResult;
import com.topc.base.model.PageParams;
import com.topc.base.model.PageResult;
import com.topc.base.utils.PageUtils;
import com.topc.participant.model.dto.QueryMenuInfoDto;
import com.topc.participant.model.dto.QueryRoleDto;
import com.topc.participant.model.po.MenuInfo;
import com.topc.participant.model.po.RoleInfo;
import com.topc.participant.model.vo.MenuShowVo;
import com.topc.participant.model.vo.MenuTreeShowVo;
import com.topc.participant.service.IMenuInfoService;
import com.topc.participant.service.IRoleInfoService;
import com.topc.participant.service.IRoleMenuMidService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/close/resource")
public class ResourceCloseController {

    @Autowired
    private IRoleInfoService roleInfoService;

    @Autowired
    private IRoleMenuMidService roleMenuMidService;

    @Autowired
    private IMenuInfoService menuInfoService;

    @GetMapping("/parent-menu")
    public CommonResult<List<MenuShowVo>> getMenuParentList() {
        List<MenuShowVo> menuParentVos = menuInfoService.getMenuShowList(1);
        return CommonResult.success(menuParentVos);
    }

    @GetMapping("/son-menu")
    public CommonResult<List<MenuShowVo>> getMenuSonList() {
        List<MenuShowVo> menuParentVos = menuInfoService.getMenuShowList(0);
        return CommonResult.success(menuParentVos);
    }

    @PostMapping("/search-menu")
    public CommonResult<PageResult<MenuInfo>> searchMenuInfoByPage(PageParams pageParams, @RequestBody QueryMenuInfoDto queryMenuInfoDto) {
        if (ObjectUtil.isNotEmpty(queryMenuInfoDto.getMenuId())) {
            return CommonResult.success(PageUtils.idPageResult(menuInfoService.getById(queryMenuInfoDto.getMenuId())));
        }
        PageResult<MenuInfo> menuInfoPageResult = menuInfoService.searchMenuInfoByPage(pageParams, queryMenuInfoDto);
        return CommonResult.success(menuInfoPageResult);
    }

    @PostMapping("/add-menu")
    public CommonResult<String> addMenuInfo(@RequestBody @Validated MenuInfo menuInfo) {
        menuInfoService.addMenuInfo(menuInfo);
        return CommonResult.success("添加成功");
    }

    @GetMapping("/del-menu")
    public CommonResult<String> delMenuInfo(Long menuId) {
        menuInfoService.delMenuInfo(menuId);
        return CommonResult.success("删除成功");
    }

    @PostMapping("/search-role")
    public CommonResult<PageResult<RoleInfo>> searchRoleInfoByPage(PageParams pageParams, @RequestBody QueryRoleDto queryRoleDto) {
        if (ObjectUtil.isNotEmpty(queryRoleDto.getRoleId())) {
            return CommonResult.success(PageUtils.idPageResult(roleInfoService.getById(queryRoleDto.getRoleId())));
        }
        PageResult<RoleInfo> roleInfoPageResult = roleInfoService.searchRoleInfoByPage(pageParams, queryRoleDto);
        return CommonResult.success(roleInfoPageResult);
    }

    @PostMapping("/add-role")
    public CommonResult<String> addRoleInfo(@RequestBody RoleInfo roleInfo) {
        roleInfoService.addRoleInfo(roleInfo);
        return CommonResult.success("添加成功");
    }

    @GetMapping("/del-role")
    public CommonResult<String> delRoleInfo(int type, Long roleId) {
        roleInfoService.delRoleInfo(type, roleId);
        return CommonResult.success("删除成功");
    }

    @GetMapping("/get-menu-role")
    public CommonResult<List<Long>> getMenuByRole(Long roleId) {
        List<Long> menuShowVos = roleMenuMidService.getMenuIdByRole(roleId);
        return CommonResult.success(menuShowVos);
    }

    @GetMapping("/tree-menu")
    public CommonResult<List<MenuTreeShowVo>> getTreeMenu() {
        List<MenuTreeShowVo> menuTree = menuInfoService.getMenuTree();
        return CommonResult.success(menuTree);
    }

    @GetMapping("/update-menu-role")
    public CommonResult<String> updateMenuRole(@RequestParam("roleId") Long roleId, @RequestParam("menuIds") List<Long> menuIds) {
        roleMenuMidService.updateMenuRole(roleId, ListUtil.toList(menuIds));
        return CommonResult.success("更新成功");
    }

    @GetMapping("/type-role")
    public CommonResult<List<RoleInfo>> getTypeRole(Integer type) {
        List<RoleInfo> roleInfos = roleInfoService.getTypeRole(type);
        return CommonResult.success(roleInfos);
    }


}
